﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money.Plugins.Import.TescoImport
{
    public partial class TescoImport : IPluginGui
    {
        void IPluginGui.Register(PluginGuiHooks pluginHooks)
        {
            ToolStripMenuItem menu;
            ToolStripMenuItem tescoMenu;
            ToolStripMenuItem creditCardSubMenu;

            // Disbable the install of plugins menu options as we are not using this for now
            return;

            tescoMenu = new ToolStripMenuItem();
            tescoMenu.Text = "Tesco Transaction Importer";
            pluginHooks.MenuHook.DropDownItems.Add(tescoMenu);

            menu = new ToolStripMenuItem();
            menu.Text = "Credit Card Categories";
            tescoMenu.DropDownItems.Add(menu);

            creditCardSubMenu = new ToolStripMenuItem();
            creditCardSubMenu.Text = "Edit";
            creditCardSubMenu.Click += CreditCardCategories_Edit;
            menu.DropDownItems.Add(creditCardSubMenu);

            ToolStripSeparator separator1 = new ToolStripSeparator();
            menu.DropDownItems.Add(separator1);

            creditCardSubMenu = new ToolStripMenuItem();
            creditCardSubMenu.Text = "Import";
            creditCardSubMenu.Enabled = false;
            //creditCardSubMenu.Click += CreditCardCategories_Edit;
            menu.DropDownItems.Add(creditCardSubMenu);

            creditCardSubMenu = new ToolStripMenuItem();
            creditCardSubMenu.Text = "Export";
            creditCardSubMenu.Enabled = false;
            creditCardSubMenu.Click += CreditCardCategories_Export;
            menu.DropDownItems.Add(creditCardSubMenu);

            creditCardSubMenu = new ToolStripMenuItem();
            creditCardSubMenu.Text = "Clear";
            creditCardSubMenu.Enabled = false;
            //creditCardSubMenu.Click += CreditCardCategories_Edit;
            menu.DropDownItems.Add(creditCardSubMenu);

            menu = new ToolStripMenuItem();
            menu.Text = "Configure Description Match";
            menu.Click += TescoTransactionMatch_Click;
            tescoMenu.DropDownItems.Add(menu);

            menu = new ToolStripMenuItem();
            menu.Text = "Configure Import Handler";
            menu.Click += (sender, e) => ChangeConfiguration();
            tescoMenu.DropDownItems.Add(menu);
        }

        private void TescoTransactionMatch_Click(object sender, EventArgs e)
        {
            TransactionMatchForm form = new TransactionMatchForm(_configuration.Item.TransactionMappings, _dataAccessInterface.Categories);

            form.Show();
            _configuration.Save();
        }

        private void ChangeConfiguration()
        {
            ConfigurationForm configGUI = new ConfigurationForm(_configuration.Item);

            if (configGUI.ShowDialog() == DialogResult.OK)
            {
                _configuration.Save();
            }
        }

        private void CreditCardCategories_Edit(object sender, EventArgs e)
        {
            LoadImportedRecords();

            CreditCardCategoriesForm creditCardCategories = new CreditCardCategoriesForm(GetTransactions, _dataAccessInterface.Categories);

            creditCardCategories._categoryMapGrid.DataSource = _configuration.Item.CategoryMappings;


            // TODO we need an option for cancelling and not saving
            creditCardCategories.ShowDialog();

            _configuration.Save();
        }

        private void CreditCardCategories_Export(object sender, EventArgs e)
        {
        }

        private void UpdateUnknownCategoryMaps(List<CategoryMapping> categoryMaps)
        {
            if (_importTransactions.Count != 0)
            {
                // The first thing we do is default all of ids to bank where they can then be customised as required.
                foreach (CategoryMapping catgeoryMap in categoryMaps)
                {
                    catgeoryMap.CategoryId = Convert.ToInt32(Categories.DefaultCategories.Blank);
                }

                // TODO This needs moved to the GUI section of the code and should also check for a valid response from form before updating
                CreditCardCategoriesForm cccatsFrm = new CreditCardCategoriesForm(SelectTransactions, _dataAccessInterface.Categories);

                cccatsFrm._categoryMapGrid.DataSource = categoryMaps.ToList();
                cccatsFrm._categoryMapGrid.Refresh();

                cccatsFrm.ShowDialog();
            }
        }
    }
}
